﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:wvru="http://ns.woodbase.dk/User.xsd" xmlns:wvrm="http://ns.woodbase.dk/Movies.xsd"
      xmlns:dt="urn:schemas-microsoft-com:datatypes"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="ArrayOfUser">
    <table class="userlist">
      <xsl:choose>
        <!-- Only apply to nodes where users has rented movies -->
        <xsl:when test="@movie='true'">
          <tr>
            <th>Client</th>
            <th>Return date</th>
            <th>Movies</th>
          </tr>
          <xsl:apply-templates select="User[./movie-rental/@rented=1]" />
        </xsl:when>
        <!-- Apply to all users -->
        <xsl:otherwise>
          <tr>
            <th>Client</th>
            <th>Details</th>
          </tr>
          <xsl:apply-templates select="User" />
        </xsl:otherwise>
      </xsl:choose>
    </table>
    <script type="text/javascript">
      $(document).ready(function(){
      $(".userDetails").click(function () {
      $(this).siblings("table").toggle("slow");
      });
      });
    </script>
  </xsl:template>

  <xsl:template match="User">
    <xsl:param name="user-id" select="@user-id" />
    <tr>
      <td class="username">
        <xsl:value-of select="@givenname"/>
        <xsl:text disable-output-escaping="yes" xml:space="preserve"> </xsl:text>
        <xsl:value-of select="@lastname"/>
      </td>

      <!-- If user has rented movies and we want the the list -->
      <xsl:if test=".//movie-rental[./@rented=1] and /@movie=true">
        <td>
          <xsl:for-each select="movie-rental[./@rented=1]">
            <table>
              <tr>
                <td>
                  <xsl:variable name="dateClean" select="substring-before(@return-date,'T')"/>
                  <xsl:variable name="dd-temp" select="substring-after($dateClean, '-')" />
                  <xsl:variable name="dd" select="substring($dd-temp, 4)" />
                  <xsl:variable name="yyyy" select="substring($dateClean,1,4)"/>
                  <xsl:variable name="MM-temp">
                    <xsl:value-of select="substring($dateClean,6)" />
                  </xsl:variable>
                  <xsl:variable name="MM" select="substring-before($MM-temp,'-')" />
                  <xsl:value-of select="$dd" />-<xsl:value-of select="$MM" />-<xsl:value-of select="$yyyy" />
                </td>
                <td>
                  <ul>
                    <xsl:for-each select="wvrm:movie">
                      <li>
                        <xsl:value-of select="@title"/>
                      </li>
                    </xsl:for-each>
                  </ul>
                </td>
              </tr>
            </table>
          </xsl:for-each>
        </td>
      </xsl:if>

      <xsl:if test="/@movie!=1 or not(@movie)">
        <td>
          <a href="#" class="userDetails">
            Details
          </a>
          <table id="{@user-id}" style="display:none;">
            <tr>
              <td>
                <h3>Current rentals</h3>
                <xsl:choose>
                  <xsl:when test=".//movie-rental/@rented=1">
                    <ul class="clean-list">
                      <xsl:for-each select="movie-rental[./@rented=1]">
                        <xsl:for-each select="wvrm:movie">
                          <li>
                            - <xsl:value-of select="@title"/><xsl:text xml:space="preserve">  </xsl:text><img src="/Images/design/delete_x.png" onclick="javascript:removeMovie('{$user-id}','{@title}'); return false;" style="cursor:hand; cursor:pointer;" title="Return movie" alt="Return movie" />
                          </li>
                        </xsl:for-each>
                      </xsl:for-each>
                    </ul>
                  </xsl:when>
                  <xsl:otherwise>
                    No current rentals
                  </xsl:otherwise>
                </xsl:choose>
              </td>
            </tr>
            <xsl:if test=".//movie-rental/@rented=0">
              <tr>
                <td>
                  <h3>Historical rentals</h3>
                  <ul class="clean-list">
                    <xsl:for-each select="movie-rental[./@rented=0]">
                      <xsl:for-each select="wvrm:movie">
                        <li>
                          - <xsl:value-of select="@title"/>
                        </li>
                      </xsl:for-each>
                    </xsl:for-each>
                  </ul>
                </td>
              </tr>
            </xsl:if>
          </table>
        </td>
      </xsl:if>
    </tr>
  </xsl:template>


</xsl:stylesheet>
